#include "../include/ftp.h"
<<<<<<< HEAD
#include "../include/sqlftp.h"
=======

>>>>>>> c0dd19eca2f7ae81c7b637b7bd48ebd771eb2007
void sig_handle(int signum)
{
    while(waitpid(-1, NULL, WNOHANG) > 0) ;
}
void sig_func(int signum)
{
    printf("disconnect\n");
}
int main(int argc, char* argv[])// conf_file path
{
    chdir("../fs");
    int pid;
    int conn_num = 0 ;
    char log_buf[128];
    char ip[16] = "" ;
    char port[10]= "";
    psession_t ps ;

    signal(13, sig_func);
    signal(17, sig_handle);
    ftp_init_from_file(argv[1], ip, port );

    socket_t fd_server = socket_server(ip, port);
    socket_t fd_client ; 
    if(fd_server == -1){
        ERR_EXIT("socket");
    }

    MYSQL *pMysql = NULL;
    sqlInit(&pMysql);
    while(1){
        fd_client = accept(fd_server, NULL, NULL);
        if(fd_client == -1){
            printf("%d\n",fd_client);
            syslog(LOG_INFO,"winftp  connect  fail");
            closelog();
            ERR_EXIT("accept");
        }
        if(0 != loginOrSign(fd_client, &pMysql)){ 
            printf("验证失败\n");
            continue;
        }
        syslog(LOG_INFO,"winftp already connect!, client=%d\n",fd_client);
        bzero(log_buf, 128);
        sprintf(log_buf, "%d connect\n",++conn_num );
        write(1, log_buf, strlen(log_buf));

        ps = (psession_t)calloc(1, sizeof(*ps));
        ps ->sess_sfd = fd_client ;
        pid = fork() ;
        if(pid == 0){
            client_handle(ps);
            exit(1);
        }
        mysql_close(pMysql);
        free(ps);
        closelog();
    }

}
